﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Db.Model.Good;
using Mate.Framework.BaseController.Admin;
using Mate.Good.Service;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Admin.Controllers.Good
{
    public class GoodCategoryController : BaseController
    {
        private readonly IGoodCategoryService goodCategoryService;
        private readonly ILog log;
        private readonly ICacheManager _cache;
        public GoodCategoryController(IGoodCategoryService goodCategoryService, ICacheManager cache, ILog log)
        {
            this.log = log;
            this.goodCategoryService = goodCategoryService;
            this._cache = cache;
        }

        public IActionResult Index()
        {
            var list = goodCategoryService.GetGoodCategoryTree();
            return View(list);
        }

        #region 添加
        public ActionResult Create()
        {

            ViewBag.GoodCategoryList = goodCategoryService.GetGoodCategoryTree(1);
            var model = new mp_good_category() { is_open = 1 };
            return View(model);
        }

        [HttpPost]
        public JsonResult Create(mp_good_category model)
        {
            var result = new BaseResult();
            model.is_delete = 1;
            var objInt = goodCategoryService.Insert(model);
            if (objInt > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "添加成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加失败";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Edit(int id)
        {
            var model = goodCategoryService.Get(x => x.id == id);
            if (model == null)
            {
                return Content("参数错误");
            }
            ViewBag.GoodCategoryList = goodCategoryService.GetGoodCategoryTree(1);
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(mp_good_category model)
        {
            var result = new BaseResult();

            var b = goodCategoryService.UpdateIgnore(model, x => new object[] { x.is_delete }, x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "修改成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "修改失败";
            }
            return Json(result);

        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var result = new BaseResult();
            var b = goodCategoryService.Update(new mp_good_category()
            {
                id = id,
                is_delete = 2
            }, x => new object[] { x.is_delete }, x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "删除成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "删除失败";
            }
            return Json(result);
        }
        #endregion
    }
}